// packageC/pages/package/gonzhuangronqiguanli/5S_manage/5S_handling/handling.js
const mybehavior = require("../../../../../../behaviors/requests")
Page({

    /**
     * 页面的初始数据
     */
    behaviors: [mybehavior],
    data: {
        fs_upload_url: "https://logisticsengineering.gree.com/scene_api/photo_manage",
        baseUrl: "https://logisticsengineering.gree.com/scene_api/fiveS_manage",
        page: 1,
        page_size: 10,
        activeNamesDetail: ['1'],
        activeNamesStep: ['1'],
        steps: [

        ],
        problem_source: "监督检查",
        btn_show:true,

        active: 0,
        now_steps: {},
        opration: {
            "问题提交": ["责任单位整改报完工", "申请延期"],
            "责任单位整改报完工": ["确认整改完成", "确认未完成整改"],
            "确认整改完成": [],
            "确认未完成整改": ["责任单位第2次整改报完工"],
            "责任单位第2次整改报完工": ["确认第二次整改完成", "确认第二次整改未完成"],
            "确认第二次整改完成": [],
            "确认第二次整改未完成": [],
            "申请延期": ["同意延期", "不同意延期"],
            "同意延期": ["责任单位整改报完工-已延期"],
            "责任单位整改报完工-已延期": ["确认整改完成-已延期", "确认未完成整改-已延期"],

            "不同意延期": ["责任单位整改报完工"],
        },
        map: {
            "问题提交": ["报完工", "申请延期"],
            "责任单位整改报完工": ["整改完成", "未整改完成"],
            "确认整改完成": [],
            "确认未完成整改": ["报完工"],
            "责任单位第2次整改报完工": ["整改完成", "未整改完成"],
            "确认第二次整改完成": [],
            "确认第二次整改未完成": [],
            "申请延期": ["同意", "不同意"],
            "同意延期": ["报完工"],
            "责任单位整改报完工-已延期": ["整改完成", "未整改完成"],

            "不同意延期": ["报完工"],
        },
        btn_list: [],
        zgwg_show: false,
        yqzg_show: false,
        wg_show: false,
        action: "",
        files: [],
        reason: "",
        deadline: "",
        set_year: 0,
        set_month: 0,
        set_day: 0
    },
    // 上传照片
    getUploaderVlaue(e) {
        let key = e.target.id
        this.setData({
            [key]: e.detail
        })
    },

    // 延期时间选择
    setPickerYearValueClick(e) {
        let rect_deadline = this.data.task_data.rect_deadline.slice(0, 10)
        let pre_rect_deadline = this.data.task_data.rect_deadline.slice(0, 10)
        if (this.data.task_data.pre_rect_deadline != null) {
            pre_rect_deadline = this.data.task_data.pre_rect_deadline.slice(0, 10)
        }
        let show_date1 = pre_rect_deadline.slice(5, 10)
        let show_date2 = e.detail.slice(5, 10)
        if (e.detail < rect_deadline) {
            wx.showToast({
                title: '整改节点需大于当前整改节点！',
                icon: 'none'
            })
            this.setData({
                date_style: "",
                deadline: pre_rect_deadline,
                deadline1: show_date1,

            })
            this.handleDateF()
        } else {
            this.setData({
                date_style: "",
                deadline: e.detail,
                deadline1: show_date2,
            })
        }

    },
    // 延期日期显示处理
    handleDateF() {
        let childObj = this.selectComponent('.my-calendar')
        childObj.dateInitSet(this.data.set_year, this.data.set_month, this.data.set_day)
    },
    // 获取待办数据
    getHandlingData() {
        let url = this.data.baseUrl + "/get_current_5s_problem"

        let data = {
            page: this.data.page,
            page_size: this.data.page_size,

        }
        this.post(url, data).then(res => {
            if (res.code == 20000) {
                this.setData({
                    list: res.data
                })
            } else {
                this.setData({
                    list: [],
                })
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        }).catch(err => {
            this.setData({
                list: [],
            })
            console.log(err);
            wx.showToast({
                title: err.msg,
                icon: 'none'
            })
        })

    },
    // 展开详情
    onActiveDetailChange(event) {
        this.setData({
            activeNamesDetail: event.detail,
        });
    },
    // 展开步骤
    onActiveChangeStep(event) {
        this.setData({
            activeNamesStep: event.detail,
        });
    },
    // 处理签审日志数据
    handleStep() {
        let live_record1 = JSON.parse(JSON.stringify(this.data.task_data.live_record))
        for (let i in live_record1) {
            live_record1[i].time = this.GMTToStr(live_record1[i].time)
        }
        let step = []
        for (let i in live_record1) {
            let obj = {
                // text: live_record1[i].action,
                desc: live_record1[i].involved_name + "，" + live_record1[i].time + "，" + live_record1[i].action,
                detail: live_record1[i]
            }
            step.push(obj)
        }
        this.setData({
            steps: step,
            active: live_record1.length - 1
        })


    },
    // 点击记录步骤详情弹窗显示
    showStep(e) {
        let step_arr = JSON.parse(JSON.stringify(this.data.steps[e.detail].detail))
        let task_data = JSON.parse(JSON.stringify(this.data.task_data))
        let arr = []
        if (step_arr.files.length != 0) {
            for (let i = 0; i < step_arr.files.length; i++) {
                let obj = {
                    url: this.data.fs_upload_url + '/fs_download?file_id=' + step_arr.files[i],
                    isImage: true,
                    file_id: step_arr.files[i]
                }
                arr.push(obj)

            }
        }
        if (step_arr.action == "申请延期") {
            this.setData({
                date: task_data.pre_rect_deadline_step,
                date_show1: true
            })

        }
        if (step_arr.action == "同意延期") {
            this.setData({
                date: task_data.rect_deadline,
                date_show1: true
            })

        }
        step_arr.files = arr
        this.setData({
            now_steps: step_arr,
            step_detail_show: true
        })

    },
    // 点击记录步骤详情弹窗关闭
    onStepClose() {
        this.setData({
            now_steps: {},
            step_detail_show: false,
            date_show1: false,
            date: "",

        })
    },
    // 处理步骤状态按钮显示
    handleStats() {
        let status = this.data.task_data.status
        let map_arr = this.data.map
        let map_keys = Object.keys(this.data.map)
        for (let i = 0; i < map_keys.length; i++) {
            if (map_keys[i] == status) {
                this.setData({
                    btn_list: map_arr[map_keys[i]]
                })
            }

        }
    },
    // 处理按钮
    handleBtn(e) {
        console.log(e);
        let index = e.currentTarget.dataset.key
        let key_str = e.currentTarget.dataset.key_str
        let opration_arr = this.data.opration
        let current_status = this.data.task_data.status
        console.log("传过去的状态", opration_arr[current_status][index]);
        this.setData({
            action: opration_arr[current_status][index]
        })
        // 整改弹窗
        if (key_str == "报完工") {
            this.setData({
                zgwg_show: true
            })
        }
        // 判断是否完成整改弹窗
        if (key_str == "整改完成" || key_str == "未整改完成" || key_str == "不同意") {
            this.setData({
                wg_show: true
            })
        }
        // 申请延期整改弹窗
        if (key_str == "同意" || key_str == "申请延期") {
            this.setData({
                yqzg_show: true
            })
        }

    },
    // 整改弹窗确认
    onConfirm1() {
        if (this.data.reason == "" || this.data.files.length == 0) {
            return wx.showToast({
                title: '说明、整改照片不能为空',
                icon: 'none'
            })
        } else {
            let url = this.data.baseUrl + "/update_5s_problem"
            let img_arr = []
            if (this.data.files.length != 0) {
                for (let i = 0; i < this.data.files.length; i++) {
                    img_arr.push(this.data.files[i].file_id);
                }
            }
            let data = {
                key: this.data.task_data.key,
                status: this.data.task_data.status,
                res_manager: this.data.task_data.res_manager,
                submitter: this.data.task_data.submitter,
                action: this.data.action,
                reason: this.data.reason,
                args: [],
                files: img_arr,
                deadline: this.data.deadline,

            }
            console.log(data);
            this.post(url, data).then(res => {
                if (res.code == 20000) {
                    wx.showToast({
                        title: res.msg,
                    })
                    if (this.data.task_data.flag == "1") {
                        setTimeout(function () {
                            wx.reLaunch({
                                url: '/packageB/pages/package/task_manager/task_manager',
                            })
                        }, 2000)
                    } else {
                        setTimeout(function () {
                            wx.reLaunch({
                                url: '/packageB/pages/package/task_manager/task_manager',
                            })
                        }, 2000)
                    }
                } else {

                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            }).catch(err => {

                console.log(err);
                wx.showToast({
                    title: err.msg,
                    icon: 'error'
                })
            })
        }
    },
    // 整改弹窗关闭
    onHandleClose1() {
        this.setData({
            zgwg_show: false,
            reason: "",
            action: "",
            files: [],

        })
    },
    //申请延期整改弹窗确认
    onConfirm2() {
        console.log(this.data.deadline);
        if (this.data.reason == "" || this.data.deadline == "") {
            return wx.showToast({
                title: '说明、延期时间不能为空',
                icon: 'none'
            })
        } else {
            let url = this.data.baseUrl + "/update_5s_problem"
            let img_arr = []
            if (this.data.files.length != 0) {
                for (let i = 0; i < this.data.files.length; i++) {
                    img_arr.push(this.data.files[i].file_id);
                }
            }
            let data = {
                key: this.data.task_data.key,
                status: this.data.task_data.status,
                res_manager: this.data.task_data.res_manager,
                submitter: this.data.task_data.submitter,
                action: this.data.action,
                reason: this.data.reason,
                args: [],
                files: img_arr,
                deadline: this.data.deadline,

            }
            console.log(data);
            this.post(url, data).then(res => {
                if (res.code == 20000) {
                    wx.showToast({
                        title: res.msg,
                    })
                    if (this.data.task_data.flag == "1") {
                        setTimeout(function () {
                            wx.reLaunch({
                                url: '/packageB/pages/package/task_manager/task_manager',
                            })
                        }, 2000)
                    } else {
                        setTimeout(function () {
                            wx.reLaunch({
                                url: '/packageB/pages/package/task_manager/task_manager',
                            })
                        }, 2000)
                    }

                } else {

                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            }).catch(err => {

                console.log(err);
                wx.showToast({
                    title: err.msg,
                    icon: 'error'
                })
            })
        }
    },
    // 申请延期整改弹窗关闭
    onHandleClose2() {
        this.setData({
            yqzg_show: false,
            action: "",
            reason: "",
        })
    },
    //申请延期整改弹窗确认
    onConfirm3() {
        if (this.data.reason == "") {
            return wx.showToast({
                title: '说明不能为空',
                icon: 'none'
            })
        } else {
            let url = this.data.baseUrl + "/update_5s_problem"
            let img_arr = []
            if (this.data.files.length != 0) {
                for (let i = 0; i < this.data.files.length; i++) {
                    img_arr.push(this.data.files[i].file_id);
                }
            }
            let data = {
                key: this.data.task_data.key,
                status: this.data.task_data.status,
                res_manager: this.data.task_data.res_manager,
                submitter: this.data.task_data.submitter,
                action: this.data.action,
                reason: this.data.reason,
                args: [],
                files: img_arr,
                deadline: this.data.deadline,

            }
            console.log(data);
            this.post(url, data).then(res => {
                if (res.code == 20000) {
                    wx.showToast({
                        title: res.msg,
                    })
                    if (this.data.task_data.flag == "1") {
                        setTimeout(function () {
                            wx.reLaunch({
                                url: '/packageB/pages/package/task_manager/task_manager',
                            })
                        }, 2000)
                    } else {
                        setTimeout(function () {
                            wx.reLaunch({
                                url: '/packageB/pages/package/task_manager/task_manager',
                            })
                        }, 2000)
                    }
                } else {

                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            }).catch(err => {

                console.log(err);
                wx.showToast({
                    title: err.msg,
                    icon: 'error'
                })
            })
        }
    },
    // 判断是否完成整改弹窗关闭
    onHandleClose3() {
        this.setData({
            wg_show: false,
            reason: "",
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.getHandlingData()
        let self = this
        const eventChannel = this.getOpenerEventChannel()
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        eventChannel.on('toHandle', function (handle_data) {
            let data = handle_data.task
            data.key = handle_data.task._id
            data.flag = "1"
            console.log("传过来的数据", data)
            data.submit_rect_deadline = self.GMTToStr(data.submit_rect_deadline)
            let arr = []
            for (let i in data.files) {
                let obj = {
                    url: self.data.fs_upload_url + '/fs_download?file_id=' + data.files[i],
                    isImage: true,
                    file_id: data.files[i]
                }
                arr.push(obj)
            }
            data.files = arr
            data.business_content = '业务内容：处理期限：' + data.rect_deadline + '；流程：' + data.status + '，责任单位：' + data.res_accurate
            if (data.pre_rect_deadline != null) {
                // pre_rect_deadline_step 签审日志所需显示日期
                data.pre_rect_deadline_step = self.GMTToStr(data.pre_rect_deadline)

                // 计算延期前一天

                const d = self.GMTToStr(data.pre_rect_deadline).slice(0, 10)
                let d1 = new Date(d);
                let prevDate = new Date(d1.getTime() - 24 * 60 * 60 * 1000);
                data.pre_rect_deadline = self.GMTToStr(prevDate)
                // 计算延期前一天
                self.setData({
                    deadline: data.pre_rect_deadline.slice(0, 10),
                    deadline1: data.pre_rect_deadline.slice(5, 10),
                    set_year: parseInt(data.pre_rect_deadline.slice(0, 5)),
                    set_month: parseInt(data.pre_rect_deadline.slice(5, 8)),
                    set_day: parseInt(data.pre_rect_deadline.slice(8, 10)),
                })
            } else {
                self.setData({
                    deadline: data.rect_deadline.slice(0, 10),
                    deadline1: data.rect_deadline.slice(5, 10),
                    set_year: parseInt(data.rect_deadline.slice(0, 5)),
                    set_month: parseInt(data.rect_deadline.slice(5, 8)),
                    set_day: parseInt(data.rect_deadline.slice(8, 10)),
                })
            }
            self.setData({
                task_data: data,
            })
            // 处理签审日志
            self.handleStep()
            // 处理状态按钮
            self.handleStats()
            self.handleDateF()
        })
        eventChannel.on('showDetail', function (handle_data) {
            let data = handle_data.task
            data.key = handle_data.task._id
            data.flag = "1"
            console.log("传过来的数据", data)

            data.submit_rect_deadline = self.GMTToStr(data.submit_rect_deadline)
            let arr = []
            for (let i in data.files) {
                let obj = {
                    url: self.data.fs_upload_url + '/fs_download?file_id=' + data.files[i],
                    isImage: true,
                    file_id: data.files[i]
                }
                arr.push(obj)
            }
            data.files = arr
            data.business_content = '业务内容：处理期限：' + data.rect_deadline + '；流程：' + data.status + '，责任单位：' + data.res_accurate
            if (data.pre_rect_deadline != null) {
                // pre_rect_deadline_step 签审日志所需显示日期
                data.pre_rect_deadline_step = self.GMTToStr(data.pre_rect_deadline)

                // 计算延期前一天

                const d = self.GMTToStr(data.pre_rect_deadline).slice(0, 10)
                let d1 = new Date(d);
                let prevDate = new Date(d1.getTime() - 24 * 60 * 60 * 1000);
                data.pre_rect_deadline = self.GMTToStr(prevDate)
                // 计算延期前一天
                self.setData({
                    deadline: data.pre_rect_deadline.slice(0, 10),
                    deadline1: data.pre_rect_deadline.slice(5, 10),
                    set_year: parseInt(data.pre_rect_deadline.slice(0, 5)),
                    set_month: parseInt(data.pre_rect_deadline.slice(5, 8)),
                    set_day: parseInt(data.pre_rect_deadline.slice(8, 10)),
                })
            } else {
                self.setData({
                    deadline: data.rect_deadline.slice(0, 10),
                    deadline1: data.rect_deadline.slice(5, 10),
                    set_year: parseInt(data.rect_deadline.slice(0, 5)),
                    set_month: parseInt(data.rect_deadline.slice(5, 8)),
                    set_day: parseInt(data.rect_deadline.slice(8, 10)),
                })
            }
            self.setData({
                task_data: data,
                btn_show:false
            })
            // 处理签审日志
            self.handleStep()
            // 处理状态按钮
            self.handleStats()
            self.handleDateF()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})